﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RepairChecker
{
    public partial class jobdone : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ResetControls();

            }
        }

        protected void dataJob_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblOfficeInfo.Text = "网点信息：" + dataJob.SelectedDataKey["DistrictName"].ToString() + dataJob.SelectedDataKey["OfficeName"].ToString();
            //lblFaultInfo.Text = "报修内容：" + dataJob.SelectedDataKey["FaultName"].ToString();

            //show fault list for change
            clstFault.Items.Clear();
            clstFault.Visible = true;
            List<Fault> faults = Entity.GetAllFaults(true);



            //维护类型的保修单
            //not maintaince type set faults radio=rlstFault .enable =true 
            //when jobdone check state by rlistfault.selectedindex

            foreach (Fault f in faults)
            {

                ListItem item = new ListItem(f.FaultName, f.FaultId.ToString());
                //item.Selected=true;


                //include maintance item
                if (f.Visible == false)
                {
                    if (dataJob.SelectedDataKey["FaultIdString"].ToString().Contains(item.Value))
                    {
                        item.Selected = true;

                        clstFault.Visible = false;

                        //break;

                    }
                }
                //add item whith out maintance item
                else
                {
                    if (dataJob.SelectedDataKey["FaultIdString"].ToString().Contains(item.Value))
                    {
                        item.Selected = true;

                        //clstFault.Visible = false;



                    }
                   
                }
                clstFault.Items.Add(item);
                ////else
                ////{
                ////    //clstFault.Enabled = false;
                ////    //呸 此举无效//目前隐藏的三种状态 主动维护 被动维护 重复 都独立不会有复选 可以直接break

                ////}



            }


            //if (faults[int.Parse(dataJob.SelectedDataKey["FaultId"].ToString())].Visible == true)
            //{
            //    clstFault.Visible = true;
            //    foreach (Fault f in faults)
            //    {
            //        if (f.Visible == true) clstFault.Items.Add(new ListItem(f.FaultName, f.FaultId.ToString()));

            //    }

            //    //clstFault.SelectedValue = dataJob.SelectedDataKey["FaultId"].ToString();
            //}
            ////
            //else
            //{
            //    clstFault.Visible = false;
            //    foreach (Fault f in faults)
            //    {
            //        clstFault.Items.Add(new ListItem(f.FaultName, f.FaultId.ToString()));

            //    }
            //    clstFault.SelectedIndex = -1;

            //}

            //确定是是否之前填过到达时间
            //填过直接填完成时间 到达时间禁用
            if (string.IsNullOrEmpty(dataJob.SelectedDataKey["ArriveDate"].ToString()))
            {
                btnArrive.Enabled = true;
                lstArriveHour.Enabled = true;
                lstArriveMinute.Enabled = true;
            }
            else
            {
                btnArrive.Enabled = false;
                lstArriveHour.Enabled = false;
                lstArriveMinute.Enabled = false;
                lstArriveHour.SelectedValue = (DateTime.Parse(dataJob.SelectedDataKey["ArriveDate"].ToString()).Hour).ToString();
                lstArriveMinute.SelectedValue = (DateTime.Parse(dataJob.SelectedDataKey["ArriveDate"].ToString()).Minute).ToString();

                lstFinishHour.Items.Clear();
                lstFinishHour.Items.Add("");
                for (int i = int.Parse(lstArriveHour.SelectedValue); i < 19; i++)
                {
                    lstFinishHour.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }

            }
            panelEditor.Enabled = true;
        }

        private void ResetControls()
        {
            lstArriveHour.Items.Clear();
            lstArriveMinute.Items.Clear();
            lstFinishHour.Items.Clear();
            lstFinishMinute.Items.Clear();
            lstArriveHour.Items.Add("");
            lstFinishHour.Items.Add("");
            for (int i = 8; i < 19; i++)
            {
                lstArriveHour.Items.Add(new ListItem(i.ToString(), i.ToString()));
                lstFinishHour.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
            lstArriveMinute.Items.Add("");
            lstFinishMinute.Items.Add("");
            for (int i = 0; i < 60; i += 5)
            {
                lstArriveMinute.Items.Add(new ListItem(i.ToString(), i.ToString()));
                lstFinishMinute.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }

            lstArriveHour.SelectedIndex = 0;
            lstArriveMinute.SelectedIndex = 0;
            lstFinishHour.SelectedIndex = 0;
            lstFinishMinute.SelectedIndex = 0;
            lblOfficeInfo.Text = "网点信息：";
            lblFaultInfo.Text = "报修内容：";
            txtFormNo.Text = "";
            lblError.Text = "";
            panelEditor.Enabled = false;
            txtMemo.Text = "";
            chkNewForm.Checked = true;
            clstFault.Visible = false;
            //clstFault.ClearSelection();
        }

        private bool IsDateDataCorrect(bool checkFinish)
        {
            if (lstArriveHour.SelectedIndex == 0)
            {
                lblError.Text = "输入时间";
                return false;
            }
            if (lstArriveMinute.SelectedIndex == 0)
            {
                lblError.Text = "输入时间";
                return false;
            }
            if (checkFinish)
            {
                if (lstFinishHour.SelectedIndex == 0)
                {
                    lblError.Text = "输入时间";
                    return false;
                }
                if (lstFinishMinute.SelectedIndex == 0)
                {
                    lblError.Text = "输入时间";
                    return false;
                }
                if (int.Parse(lstFinishHour.Text) < int.Parse(lstArriveHour.Text))
                {
                    lblError.Text = "检查完成时间";
                    return false;
                }
                if (int.Parse(lstFinishHour.Text) == int.Parse(lstArriveHour.Text))
                {
                    if (int.Parse(lstFinishMinute.Text) < int.Parse(lstArriveMinute.Text))
                    {
                        lblError.Text = "检查完成时间";
                        return false;
                    }
                }
            }

            return true;
        }

        protected void btnFinish_Click(object sender, EventArgs e)
        {
            string faultIdString;

            if (!IsDateDataCorrect(true))
            {
                return;
            }

            if (chkNewForm.Checked)
            {
                if (txtMemo.Text.Length == 0)
                {
                    lblError.Text = "必须填写进一步处理的理由";
                    return;
                }
            }

            DateTime arriveTime = DateTime.Now;
            DateTime finishTime = DateTime.Now;


            faultIdString = string.Empty;
            foreach (ListItem item in clstFault.Items)
            {
                if (item.Selected == true)
                {
                    faultIdString += item.Value + ',';
                }
            }

            if (faultIdString.Length==0)
            {
                lblError.Text = "神经病";
                return;
            }

            int ret =
                Entity.JobDone(
                    (int)dataJob.SelectedDataKey["FormId"],
                    new DateTime(
                        DateTime.Now.Year,
                        DateTime.Now.Month,
                        DateTime.Now.Day,
                        int.Parse(lstArriveHour.SelectedValue),
                        int.Parse(lstArriveMinute.SelectedValue),
                        0),
                    new DateTime(
                        DateTime.Now.Year,
                        DateTime.Now.Month,
                        DateTime.Now.Day,
                        int.Parse(lstFinishHour.SelectedValue),
                        int.Parse(lstFinishMinute.SelectedValue), 0
                        ),
                    txtFormNo.Text, faultIdString);

            if (ret == -1)
            {
                lblError.Text = "更新出错";
            }
            else
            {
                //repair once more
                if (chkNewForm.Checked)
                {
                    faultIdString = string.Empty;
                    foreach (ListItem item in clstFault.Items)
                    {
                        if (item.Selected == true)
                        {
                            faultIdString += item.Value + ',';
                        }
                    }
                    ret = Entity.AddJob(
                            int.Parse(dataJob.SelectedDataKey["OfficeId"].ToString()),
                            dataJob.SelectedDataKey["FaultIdString"].ToString(),
                            Entity.JobType.Repair, txtMemo.Text);
                    if (ret == -1)
                    {
                        lblError.Text = "新增报修记录失败";
                        return;
                    }
                }


                ResetControls();
                dataJob.DataBind();
            }

        }

        protected void btnArrive_Click(object sender, EventArgs e)
        {
            if (!IsDateDataCorrect(false))
            {
                return;
            }

            string faultIdString = string.Empty;
            foreach (ListItem item in clstFault.Items)
            {
                if (item.Selected == true)
                {
                    faultIdString += item.Value + ',';
                }
            }

            int ret =
                Entity.JobDone(
                    (int)dataJob.SelectedDataKey["FormId"],
                    new DateTime(
                    DateTime.Now.Year,
                    DateTime.Now.Month,
                    DateTime.Now.Day,
                    int.Parse(lstArriveHour.SelectedValue),
                    int.Parse(lstArriveMinute.SelectedValue),
                    0),
                    DateTime.MinValue,
                    txtFormNo.Text,
                    faultIdString);
            if (ret == -1)
            {
                lblError.Text = "设置到达时间出错";
            }
            else
            {
                ResetControls();
                dataJob.DataBind();
            }

        }


        protected void dataJob_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                Job job = (Job)e.Row.DataItem;

                if (!String.IsNullOrEmpty(job.FaultIdString))
                {
                    BulletedList _lstFaults = (BulletedList)e.Row.FindControl("lstFaults");
                    List<Fault> faults = Entity.GetAllFaults(true);

                    foreach (string s in job.FaultIdString.Split(','))
                    {
                        if (!string.IsNullOrEmpty(s)) //
                        {
                            foreach (Fault f in faults)
                            {
                                if (f.FaultId == int.Parse(s))
                                {
                                    _lstFaults.Items.Add(f.FaultName);
                                    break;
                                }
                            }
                        }

                    }
                }

            }


        }
    }
}